/* Cermelo Woodgett
 *
 * SelectionSort.java
 *
 * Created on January 25, 2007, 10:09 PM
 *
 * Sorts items in Array into ascending order.
 *
 * Precondition: The array is an array of n items
 *
 * Postcondition: The Array is sorted in asceding order
 *
 * Original code found in "Data Abstraction & Problem Soving with Java"
 * 2005., Carrano, F., Prichard, J.; pg. 478
 *
 */

public class SelectionSort {
    
    public static void SelSort(int[] theArray, int n) {
        
        /** Creates a new instance of SelectionSort */
        for (int last = n - 1; last >= 1; last--) {
            
            int largest = indexOfLargest(theArray, last+1);
            
            int temp = theArray[largest];
            theArray[largest] = theArray[last];
            theArray[last] = temp;
        }
    }
    
    private static int indexOfLargest(int[] theArray, int size) {
        
        int indexSoFar = 0;  // index of largest item so far
        
        for (int currIndex = 1; currIndex < size; ++currIndex) {
            
            if (theArray[currIndex] < theArray[indexSoFar]) {
                indexSoFar = currIndex;
            }
        }
        
        return indexSoFar; // index of largest item
    } // end indexOfLargest
}